package com.leadingsoft.pwxk.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leadingsoft.pwxk.dto.LicenseInfoDto;
import com.leadingsoft.pwxk.dto.PagingTableDto;
import com.leadingsoft.pwxk.dto.PwxkzBasicDto;
import com.leadingsoft.pwxk.dto.SearchDto;
import com.leadingsoft.pwxk.enums.WPType;
import com.leadingsoft.pwxk.excel.CommonHandleExcel;
import com.leadingsoft.pwxk.excel.ExcelUtil;
import com.leadingsoft.pwxk.service.LicenseInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author ysh
 *
 */
@Api(description = "排污许可证核发信息相关")
@RestController
@RequestMapping("api/pwxuzInfo")
public class PwxkzInfoController {

	@Autowired
	LicenseInfoService licenseInfoService;

	@ApiOperation("排污许可证核发信息-列表")
	@GetMapping(params = { "draw" })
	@ResponseStatus(code = HttpStatus.OK)
	public PagingTableDto<LicenseInfoDto> getList(SearchDto searchDto,
			@RequestParam(value = "draw", required = false) Long draw) {
		PagingTableDto<LicenseInfoDto> list = licenseInfoService.getList(searchDto);
		list.setDraw(draw);
		return list;
	}

	@ApiOperation("排污许可证核发信息-基本信息")
	@GetMapping(path = "{dataid}")
	@ResponseStatus(code = HttpStatus.OK)
	public PwxkzBasicDto getBasic(@PathVariable(value = "dataid") String dataId) {
		return licenseInfoService.getBasic(dataId);
	}

	@ApiOperation("排污许可统计 - 通用导出 excel")
	@RequestMapping(value = "export", method = RequestMethod.GET)
	public void listExcel(HttpServletResponse response, SearchDto searchDto,
			@RequestParam(value = "title", required = false, defaultValue = "排污许可统计") String title) {
		List<Map<String, Object>> datas = getData(searchDto);
		List<String> titleFirst = new ArrayList<>();
		List<String> propertyList = Arrays
				.asList(new String[] { "no", "nameRegion", "xkzNum", "devcompany", "hyname" });
		List<String> titleList = Arrays.asList(new String[] { "序号", "地区", "许可证编号", "单位名称", "行业类别" });
		propertyList = new ArrayList<String>(propertyList);
		titleList = new ArrayList<String>(titleList);
		insertKey(searchDto.getWrwType(), propertyList, titleList);
		for (int i = 0; i < propertyList.size(); i++) {
			titleFirst.add(title);
		}
		List<String[]> heads = new ArrayList<>();
		heads.add(titleFirst.toArray(new String[] {}));
		heads.add(titleList.toArray(new String[] {}));
		ExcelUtil.createWorkBook(title, heads, propertyList.toArray(new String[] {}), datas, response,
				new CommonHandleExcel(propertyList.size(), datas.size()), null);
	}

	private void insertKey(WPType wpType, List<String> propertyList, List<String> titleList) {
		HashMap<String, String> proMap = new HashMap<>();

		// 水/大气 污染排放量统计 - 字段
		if (wpType == WPType.GAS || wpType == WPType.WATER) {

			if (wpType == WPType.GAS) {
				proMap.put("so2", "SO2");
				proMap.put("nox", "NOx");
				proMap.put("pm", "颗粒物");
			} else {
				proMap.put("codcr", "CODcr");
				proMap.put("ad", "氨氮");
			}
		} else {
			// 排污许可证发放量 - 字段
			proMap.put("times", "有效期限");
			proMap.put("fztime", "发证日期");
		}
		for (String key : proMap.keySet()) {
			propertyList.add(key);
			titleList.add(proMap.get(key));
		}
		proMap = null;
	}

	private List<Map<String, Object>> getData(SearchDto searchDto) {
		ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
		searchDto.setSize(Integer.MAX_VALUE);
		List<LicenseInfoDto> list = licenseInfoService.getList(searchDto).getData();
		Integer num = 1;
		for (LicenseInfoDto dto : list) {
			JSONObject jsonStr = (JSONObject) JSON.toJSON(dto);
			@SuppressWarnings("unchecked")
			Map<String, Object> mapStr = (Map<String, Object>) JSON.parse(jsonStr.toJSONString());
			mapStr.put("no", num++);
			data.add(mapStr);
		}
		num = null;
		list = null;
		return data;
	}
}
